import { Model, DataTypes, Optional } from 'sequelize';
import sequelize from '@/config/database';

// 定义表字段属性接口
interface GenTableColumnAttributes {
  columnId: number;
  tableId: number;
  columnName: string;
  columnComment: string;
  columnType: string;
  javaType: string;
  javaField: string;
  isPk?: string;
  isIncrement?: string;
  isRequired?: string;
  isInsert?: string;
  isEdit?: string;
  isList?: string;
  isQuery?: string;
  queryType?: string;
  htmlType?: string;
  dictType?: string;
  sort?: number;
  createBy?: string;
  createTime?: Date;
  updateBy?: string;
  updateTime?: Date;
}

// 创建时可选的字段
interface GenTableColumnCreationAttributes extends Optional<GenTableColumnAttributes, 'columnId'> {}

// 表字段模型类
class GenTableColumn extends Model<GenTableColumnAttributes, GenTableColumnCreationAttributes> implements GenTableColumnAttributes {
  public columnId!: number;
  public tableId!: number;
  public columnName!: string;
  public columnComment!: string;
  public columnType!: string;
  public javaType!: string;
  public javaField!: string;
  public isPk?: string;
  public isIncrement?: string;
  public isRequired?: string;
  public isInsert?: string;
  public isEdit?: string;
  public isList?: string;
  public isQuery?: string;
  public queryType?: string;
  public htmlType?: string;
  public dictType?: string;
  public sort?: number;
  public createBy?: string;
  public createTime?: Date;
  public updateBy?: string;
  public updateTime?: Date;

  // 时间戳
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;

  // 获取TypeScript类型
  public getTypeScriptType(): string {
    if (this.javaType === 'String') {
      return 'string';
    } else if (this.javaType === 'Integer' || this.javaType === 'Long') {
      return 'number';
    } else if (this.javaType === 'Double' || this.javaType === 'Float') {
      return 'number';
    } else if (this.javaType === 'Boolean') {
      return 'boolean';
    } else if (this.javaType === 'Date') {
      return 'Date';
    } else if (this.javaType === 'BigDecimal') {
      return 'number';
    } else {
      return 'any';
    }
  }

  // 获取Sequelize字段类型
  public getSequelizeType(): string {
    if (this.javaType === 'String') {
      if (this.columnType.includes('text')) {
        return 'DataTypes.TEXT';
      } else {
        const length = this.getColumnLength();
        return `DataTypes.STRING(${length})`;
      }
    } else if (this.javaType === 'Integer') {
      return 'DataTypes.INTEGER';
    } else if (this.javaType === 'Long') {
      return 'DataTypes.BIGINT';
    } else if (this.javaType === 'Double') {
      return 'DataTypes.DOUBLE';
    } else if (this.javaType === 'Float') {
      return 'DataTypes.FLOAT';
    } else if (this.javaType === 'Boolean') {
      return 'DataTypes.BOOLEAN';
    } else if (this.javaType === 'Date') {
      return 'DataTypes.DATE';
    } else if (this.javaType === 'BigDecimal') {
      return 'DataTypes.DECIMAL';
    } else {
      return 'DataTypes.STRING';
    }
  }

  // 从列类型中提取长度
  private getColumnLength(): number {
    const matches = this.columnType.match(/\((\d+)\)/);
    return matches ? parseInt(matches[1]) : 255;
  }

  // 检查字段是否为主键
  public isPrimaryKey(): boolean {
    return this.isPk === '1';
  }

  // 检查字段是否自增
  public isAutoIncrement(): boolean {
    return this.isIncrement === '1';
  }

  // 检查字段是否必填
  public isRequiredField(): boolean {
    return this.isRequired === '1';
  }
}

GenTableColumn.init(
  {
    columnId: {
      type: DataTypes.BIGINT,
      primaryKey: true,
      autoIncrement: true,
      field: 'column_id'
    },
    tableId: {
      type: DataTypes.BIGINT,
      allowNull: false,
      field: 'table_id'
    },
    columnName: {
      type: DataTypes.STRING(200),
      allowNull: false,
      field: 'column_name'
    },
    columnComment: {
      type: DataTypes.STRING(500),
      field: 'column_comment'
    },
    columnType: {
      type: DataTypes.STRING(100),
      field: 'column_type'
    },
    javaType: {
      type: DataTypes.STRING(100),
      field: 'java_type'
    },
    javaField: {
      type: DataTypes.STRING(100),
      field: 'java_field'
    },
    isPk: {
      type: DataTypes.STRING(1),
      field: 'is_pk'
    },
    isIncrement: {
      type: DataTypes.STRING(1),
      field: 'is_increment'
    },
    isRequired: {
      type: DataTypes.STRING(1),
      field: 'is_required'
    },
    isInsert: {
      type: DataTypes.STRING(1),
      field: 'is_insert'
    },
    isEdit: {
      type: DataTypes.STRING(1),
      field: 'is_edit'
    },
    isList: {
      type: DataTypes.STRING(1),
      field: 'is_list'
    },
    isQuery: {
      type: DataTypes.STRING(1),
      field: 'is_query'
    },
    queryType: {
      type: DataTypes.STRING(200),
      field: 'query_type'
    },
    htmlType: {
      type: DataTypes.STRING(200),
      field: 'html_type'
    },
    dictType: {
      type: DataTypes.STRING(200),
      field: 'dict_type'
    },
    sort: {
      type: DataTypes.INTEGER,
      field: 'sort'
    },
    createBy: {
      type: DataTypes.STRING(64),
      field: 'create_by'
    },
    createTime: {
      type: DataTypes.DATE,
      field: 'create_time'
    },
    updateBy: {
      type: DataTypes.STRING(64),
      field: 'update_by'
    },
    updateTime: {
      type: DataTypes.DATE,
      field: 'update_time'
    }
  },
  {
    sequelize,
    tableName: 'gen_table_column',
    timestamps: true,
    createdAt: 'createTime',
    updatedAt: 'updateTime',
    hooks: {
      beforeCreate: (column: GenTableColumn) => {
        if (!column.createTime) {
          column.createTime = new Date();
        }
      },
      beforeUpdate: (column: GenTableColumn) => {
        column.updateTime = new Date();
      }
    }
  }
);

export default GenTableColumn; 